﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Threading;
using System.Windows.Threading;
using NAudio;
using System.Drawing;
using WPFRadio;




namespace WPFRadio
{
	/// <summary>
	/// Logique d'interaction pour MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{

		#region Declarations 
		
		private static Mutex mutex;
		DispatcherTimer timer;
		private bool AnimationPlayed = false;
		private System.Windows.Forms.NotifyIcon radioTray;
		private System.Windows.Forms.ContextMenu ConMenu;
		private System.Windows.Forms.MenuItem MenuPlayItem;
		private System.Windows.Forms.MenuItem MenuCloseItem;
		private System.Windows.Forms.MenuItem MenuOptionItem;
		#endregion



		public MainWindow()
		{
			this.InitializeComponent();

			timer = new DispatcherTimer();
			timer.Tick += new EventHandler(timer_Tick);
			timer.Interval = new TimeSpan(0, 0, 5);
			timer.Start();
			// Insérez le code requis sur la création de l'objet à partir de ce point.
			SetPos();
			#region Creating new Menu
		   
			radioTray  = new System.Windows.Forms.NotifyIcon();
			ConMenu    = new System.Windows.Forms.ContextMenu();
			ConMenu.Name = "Menu";
			MenuPlayItem   = new System.Windows.Forms.MenuItem("Play");
			MenuOptionItem = new System.Windows.Forms.MenuItem("Option");
			MenuCloseItem  = new System.Windows.Forms.MenuItem("Exit");

			#endregion

			#region Event Handler for the menus
		   
			MenuPlayItem.Click    += new EventHandler(MenuPlayItem_Click);
			MenuCloseItem.Click   += new EventHandler(MenuCloseItem_Click);
			MenuOptionItem.Click  += new EventHandler(MenuOptionItem_Click);
			radioTray.DoubleClick += new EventHandler(radioTray_DoubleClick);
			#endregion

            
			ConMenu.MenuItems.Add(MenuPlayItem);
			ConMenu.MenuItems.Add(MenuOptionItem);
			ConMenu.MenuItems.Add(MenuCloseItem);

			#region Tray Icon
			radioTray.ContextMenu = ConMenu;
			radioTray.BalloonTipText = "Hello";
			radioTray.Icon = new Icon("clef.ico");
			radioTray.Visible = true;
			radioTray.Text = "WPFRadio";
			#endregion

			#region Only One Instance
			
			bool createNew = false;

			mutex = new Mutex(false,"Local\\WPFRADIO",out createNew);

			if (!createNew)
			{
				MessageBox.Show("WPFRadio is already running.", "WPFRadio", MessageBoxButton.OK, MessageBoxImage.Asterisk);
				this.Close();

			}
			#endregion




		}

		void MenuOptionItem_Click(object sender, EventArgs e)
		{

            PlaylistManager PlayerManager = new PlaylistManager();
            PlayerManager.Show();


		}

		void radioTray_DoubleClick(object sender, EventArgs e)
		{
			if (this.IsVisible == false)
				this.Show();
		}

		void MenuCloseItem_Click(object sender, EventArgs e)
		{
			this.Close();

		}

		void MenuPlayItem_Click(object sender, EventArgs e)
		{

		   

			//

	   
		}

		  

  
		void timer_Tick(object sender, EventArgs e)
		{

			
			this.Hide();

		}


		private void SetPos()
		{


			double height = SystemParameters.PrimaryScreenHeight;
			double width = SystemParameters.PrimaryScreenWidth;

			this.Left = width - this.Width;
			this.Top  = height - this.Height-35;
			
		 


		}

		private void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
		{
			
			
			
		}

		private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			this.Show();
		   
			this.Close();
			
		}

		private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
		{
			// TODO : ajoutez ici l’implémentation du gestionnaire d’événements.
			if (AnimationPlayed == false)
			{

				e.Cancel = true;
				
				AnimationPlayed = true;
				Hide_BeginStoryboard.Storyboard.Completed += new EventHandler(Storyboard_Completed);
				Hide_BeginStoryboard.Storyboard.Begin();
			   
				
			}
		}

		void Storyboard_Completed(object sender, EventArgs e)
		{

			radioTray.Visible = false;
            Application.Current.Shutdown();



        }
	}
}